package com.zuikaku.controller;

import com.zuikaku.pojo.Order;
import com.zuikaku.service.OrderService;
import com.zuikaku.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/getOrdersByItemIdAndUserId/{userId}/{itemId}")
    public String getOrdersByItemIdAndUserId(@PathVariable("userId")Integer userId, @PathVariable(value = "itemId",required = false)Integer itemId, Model model){
        List<Order> orders = orderService.getOrdersByItemIdAndUserId(userId,itemId);
        model.addAttribute("orderList",orders);
        return "home";
    }

    @ResponseBody
    @GetMapping("/getOrdersByIds/{userIds}")
    public List<Order> getOrdersByUserIds(@PathVariable("userIds")String ids){
        String[] idsStrs= ids.split(",");
        return orderService.getOrdersByIds( Arrays.stream(idsStrs).map(Integer::parseInt).collect(Collectors.toList()));
    }

    @ResponseBody
    @GetMapping("/getOrderVOByPk/{id}")
    public OrderVO getOrderVOByPk(@PathVariable("id")Integer id){
        return orderService.getOrderVOByPk(id);

    }

    @ResponseBody
    @GetMapping("/getOrderVOByPage/{pageCount}/{pageSize}")
    public List<OrderVO> getOrderVOByPage(@PathVariable("pageCount")Integer pageCount,@PathVariable("pageSize")Integer pageSize){
        return orderService.queryOrderVOByPage(pageCount,pageSize);

    }
}
